﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.ITheApplicationFormB;
using StartsEAS_API.DAL.ResultsD;
using StartsEAS_API.DAL.ScheduleD;
using StartsEAS_API.DAL.Student;
using StartsEAS_API.DAL.TheApplicationFormD;
using StartsEAS_API.DAL.WorkerD;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.TheApplicationFormB
{
    public class TheApplicationFormBLL: ITheApplicationFormBLL
    {
        private readonly StarsEASContext _context;
        public TheApplicationFormDAL _theApplicationFormDal;
        public StudentDAL _studentDal;
        public WorkerDAL _workerDal;
        public ResultsDAL _resultDal;
        public ScheduleDAL _scheduleDal;
        public TheApplicationFormBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _theApplicationFormDal = new(_context);
            _studentDal = new(_context);
            _workerDal = new(_context);
            _resultDal = new(_context);
            _scheduleDal = new(_context);
        }
        /// <summary>
        /// 老师审批
        /// </summary>
        /// <param name="token">老师token</param>
        /// <param name="status">1 同意 2 拒绝</param>
        /// <param name="applicationFormId">消息审批申请表Id</param>
        /// <returns></returns>
        public ResultUtils AlterStuCounselorApproval(string token, int status, int applicationFormId)
        {
            return _theApplicationFormDal.AlterStuCounselorApproval(token, status, applicationFormId);
        }
        /// <summary>
        /// 教务处审批
        /// </summary>
        ///<param name="token">教务处token</param>
        /// <param name="status">1 同意 2 拒绝</param>
        /// <param name="theApplicationFormJSON">消息审批申请表</param>
        /// <returns></returns>
        public ResultUtils AlterStuDeptApproval(string token, int status, JObject theApplicationFormJSON)
        {
            if (theApplicationFormJSON == null) return new ResultUtils() { code = 500, message = "theApplicationForm为空" };
            string jsonStrs = JsonConvert.SerializeObject(theApplicationFormJSON);
            TheApplicationForm theApplicationForm = JsonConvert.DeserializeObject<TheApplicationForm>(jsonStrs);
            if (theApplicationForm == null) return new ResultUtils() { code = 500, message = "applicationFormId有误" };
            List<SetOperationResult> ResultList = new List<SetOperationResult>();
            AppLog.WriteInfo("当前登录用户："+token, true);
            AppLog.WriteInfo("审批状态："+status, true);
            Console.WriteLine("当前登录用户：" + token);
            Console.WriteLine("审批状态：" + status);
            if (status == 1)
            {
                ResultUtils fromResult = _theApplicationFormDal.AlterStuDeptApproval(token, status, theApplicationForm);
                ResultList.Add(new SetOperationResult() { Id = "审批表操作", message = fromResult });
                if (fromResult.code == 200)
                {
                    List<Model.Models.Student> stuList = new List<Model.Models.Student>();
                    List<Worker> worList = new List<Worker>();
                    List<Result> results = new List<Result>();
                    List<SetOperationResult> setOperationResultList = new List<SetOperationResult>();
                    Console.WriteLine("InfoJson："+ theApplicationForm.InfoJson);
                    JArray array = JArray.Parse(theApplicationForm.InfoJson);
                    switch (theApplicationForm.ApplicationType)
                    {
                        case (int)AlterApplicationType.AddStudentInformation:
                            //添加学生
                            foreach (JObject data in array)
                            {
                                string itemStr = JsonConvert.SerializeObject(data);
                                Model.Models.Student item = JsonConvert.DeserializeObject<Model.Models.Student>(itemStr);
                                ResultUtils result = _studentDal.InsertStudent(item);
                                if (result.message.Equals(2))
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "scucess" });
                                else if (result.message.Equals(1))
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "账号密码或学生个人信息出现异常" });
                                else
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "error" });
                            }
                            break;
                        case (int)AlterApplicationType.ModifyStudentInformation:
                            //修改学生信息
                            foreach (JObject data in array)
                            {
                                string itemStr = JsonConvert.SerializeObject(data);
                                Model.Models.Student item = JsonConvert.DeserializeObject<Model.Models.Student>(itemStr);
                                ResultUtils result = _studentDal.UpdateStudent(item);
                                if (result.message.Equals(1))
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "scucess" });
                                else
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "error" });
                            }
                            break;
                        case (int)AlterApplicationType.ModifyingWorkerInformation:
                            //修改职工信息
                            foreach (JObject data in array)
                            {
                                string itemStr = JsonConvert.SerializeObject(data);
                                Worker item = JsonConvert.DeserializeObject<Worker>(itemStr);
                                ResultUtils result = _workerDal.UpdateWorker(item);
                                if (result.message.Equals(1))
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.WorkerId, message = "scucess" });
                                else
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.WorkerId, message = "error" });
                            }
                            break;
                        case (int)AlterApplicationType.TheClassesFor:
                            //调课
                            foreach (JObject data in array)
                            {
                                string itemStr = JsonConvert.SerializeObject(data);
                                Schedule item = JsonConvert.DeserializeObject<Schedule>(itemStr);
                                int result = _scheduleDal.AdjustTheCourse(item);
                                if (result > 0)
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.ScheduleId.ToString(), message = "scucess" });
                                else
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.ScheduleId.ToString(), message = "error" });
                            }
                            break;
                        case (int)AlterApplicationType.GradeEntryInformation:
                            //添加成绩信息
                            foreach (JObject data in array)
                            {
                                string itemStr = JsonConvert.SerializeObject(data);
                                Result item = JsonConvert.DeserializeObject<Result>(itemStr);
                                int result = _resultDal.AddResults(item);
                                if (result > 0)
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "scucess" });
                                else
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "error" });
                            }
                            break;
                        case (int)AlterApplicationType.ModifyGradeInformation:
                            //修改成绩信息
                            foreach (JObject data in array)
                            {
                                string itemStr = JsonConvert.SerializeObject(data);
                                Result item = JsonConvert.DeserializeObject<Result>(itemStr);
                                int result = _resultDal.UpdateResults(item);
                                if (result > 0)
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "scucess" });
                                else
                                    setOperationResultList.Add(new SetOperationResult() { Id = item.StuId, message = "error" });
                            }
                            break;
                    }
                    ResultList.Add(new SetOperationResult() { Id = "实体数据操作", message = setOperationResultList });
                }
            }
            else if (status == 2)
            {
                ResultUtils fromResult = _theApplicationFormDal.AlterStuDeptApproval(token, status, theApplicationForm);
                ResultList.Add(new SetOperationResult() { Id = "审批表操作", message = fromResult });
            }
            else 
                return new ResultUtils() { code = 500, message = "审批状态提交有误" };
            return new ResultUtils() { code = 200, message = ResultList };
        }

        /// <summary>
        /// 教务处查询所有的申请表
        /// </summary>
        /// <param name="token">教务处Token</param>
        /// <returns></returns>
        public ResultUtils GetAllApplicationForm(string token)
        {
            return _theApplicationFormDal.GetAllApplicationForm(token);
        }
        /// <summary>
        /// 根据Token查询该用户的所有申请表
        /// </summary>
        /// <param name="token">用户token(学生、老师)</param>
        /// <returns></returns>
        public ResultUtils GetAllApplicationFormByToken(string token)
        {
            return _theApplicationFormDal.GetAllApplicationFormByToken(token);
        }
        /// <summary>
        /// 添加一条申请表信息
        /// </summary>
        /// <param name="token">用户token</param>
        /// <param name="InfoJSON">申请表具体信息</param>
        /// <param name="ApplicationType">申请类型</param>
        /// <param name="Status">当前状态</param>
        /// <returns></returns>
        public ResultUtils InsertApplicationForm(string token, string InfoJSON, int ApplicationType, int Status)
        {
            return _theApplicationFormDal.InsertApplicationForm(token, InfoJSON, ApplicationType, Status);
        }

        public ResultUtils GetStudentApplicationFormByWorkerToken(string token, string classId) { 
            return _theApplicationFormDal.GetStudentApplicationFormByWorkerToken(token, classId);
        }
    }
}
